package com.csii.activiti.server.core.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.activiti.server.core.entity.*;
import com.csii.activiti.server.core.query.QueryActFlowTask;
import com.csii.activiti.server.core.service.FlowManagementService;
import com.csii.activiti.server.core.service.FlowOperationService;
import com.csii.activiti.server.core.service.FlowQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class FlowManagementServiceImpl implements FlowManagementService {

    @Autowired
    private FlowQueryService flowQueryService;
    @Autowired
    private FlowOperationService flowOperationService;

    @Override
    public Page<ActFlowTodo> queryTaskList(QueryActFlowTask queryActFlowTask, Page page) {
        return flowQueryService.queryTodoList(queryActFlowTask,page,queryActFlowTask.getProcessUsername());
    }

    @Override
    public void batchCompleteTask(ActBatchOperation actBatchOperation) {
        for (String taskId : actBatchOperation.getTaskIds()) {
            ActCompleteTask actCompleteTask = new ActCompleteTask();
            actCompleteTask.setTaskId(taskId);
            actCompleteTask.setCommand(actBatchOperation.getCommand());
            try{
                flowOperationService.completeTask(actCompleteTask);
            }catch (Exception e){
                log.error("error is batchCompleteTask error:{}",e);
            }
        }
    }

    @Override
    public void batchDelegateTask(ActBatchDelegateTask batchDelegateTask) {
        for (String taskId : batchDelegateTask.getTaskIds()) {
            flowOperationService.delegateTask(new ActDelegateTask(taskId,batchDelegateTask.getUsernameDelegate()));
        }
    }

    @Override
    public void batchBackTask(ActBatchOperation actBatchOperation) {
        for (String taskId : actBatchOperation.getTaskIds()) {
            ActBackTask actBackTask = new ActBackTask();
            actBackTask.setTaskId(taskId);
            actBackTask.setCommand(actBatchOperation.getCommand());
            try{
                flowOperationService.backTask(actBackTask);

            }catch (Exception e){
                log.error("error is batchBackTask error:{}",e);
            }
        }
    }
}
